package com.learn.springbootnetty.netdemo;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * InetAddress  获取ip信息。根据ip获取该ip的组播，回环等相关信息
 * @author xiaodonghong
 * @description TODO
 * @date 2023/6/12-15:50
 */
public class InetAddressDemo {
    public static void main(String[] args) throws IOException {
        demo1();
      //  test2();
    }
    public static void test1() throws UnknownHostException {
        System.out.println("  -------------  test1    ---------------------");
        InetAddress inet4Address = Inet4Address.getByAddress("192.168.33.7".getBytes());
        System.out.println("inet4Address.getAddress() = "+ inet4Address.getAddress());
        System.out.println(inet4Address.isLoopbackAddress());
        System.out.println(inet4Address.getCanonicalHostName());
        System.out.println(inet4Address.isMulticastAddress());
        System.out.println(inet4Address.isLinkLocalAddress());
        System.out.println(inet4Address.isMCGlobal());
        System.out.println("inet4Address.getHostName() = "+ inet4Address.getHostName());
        System.out.println("inet4Address.getHostAddress() = "+ inet4Address.getHostAddress());
    }

    public static void test2() throws UnknownHostException {
        System.out.println("        -------------  test2    ---------------------");
        InetAddress inet4Address = Inet4Address.getByAddress("XIAODONGHONG-PC".getBytes());
        System.out.println("inet4Address.getAddress() = "+ inet4Address.getAddress());
        System.out.println(inet4Address.isLoopbackAddress());
        System.out.println(inet4Address.getCanonicalHostName());
        System.out.println(inet4Address.isMulticastAddress());
        System.out.println(inet4Address.isLinkLocalAddress());
        System.out.println(inet4Address.isMCGlobal());
        System.out.println("inet4Address.getHostName() = "+ inet4Address.getHostName());
        System.out.println("inet4Address.getHostAddress() = "+ inet4Address.getHostAddress());
    }

    public static void demo1() throws IOException {
        /*
             static InetAddress getByName(String host) 确定主机名称的IP地址。
                    主机名称可以是机器名称，也可以是IP地址
             String getHostName()       获取此IP地址的主机名
             String getHostAddress()   返回文本显示中的IP地址字符串
         */

        // 1.获取InetAddress的对象,表示IP的对象
//    IP又表示电脑,所以说这个类的对象可以看作是一台电脑的对象，它表示的就是网络中的某一台电脑
        InetAddress  address = InetAddress.getByName("127.0.0.1");
        System.out.println(address); // /192.168.1.100
        System.out.println( address.getHostAddress());
        System.out.println(address.getHostName());
        System.out.println(address.getAddress());
        System.out.println(address.getCanonicalHostName());
        System.out.println(address.isLoopbackAddress());
        System.out.println(address.isLinkLocalAddress());
        System.out.println(address.isMCGlobal());
        System.out.println(address.isSiteLocalAddress());

            // 1.获取本机地址对象。
            InetAddress ip1 = InetAddress.getLocalHost();
            System.out.println(ip1.getHostName()); // DESKTOP-U9AANQN
            System.out.println(ip1.getHostAddress()); // 192.168.238.203

            // 2.获取域名ip对象
            InetAddress ip2 = InetAddress.getByName("www.baidu.com");
            System.out.println(ip2.getHostName()); // www.baidu.com
            System.out.println(ip2.getHostAddress()); // 220.181.38.149

            // 3.获取公网IP对象。
            InetAddress ip3 = InetAddress.getByName("220.181.38.149");
            System.out.println(ip3.getHostName()); // 112.80.248.76
            System.out.println(ip3.getHostAddress()); // 112.80.248.76

            // 4.判断是否能通： ping  5s之前测试是否可通
            System.out.println(ip3.isReachable(5000)); // true


        InetAddress address1 = InetAddress.getByName("DESKTOP-U9AANQN");
        System.out.println(address1); // ROG/192.168.56.1
        System.out.println(address == address1); // false

        // 2.获取电脑的主机名
        // 注意:如果说你的电脑因为网络原因或者是局域网当中就没有这台电脑，那么此时是获取不到它的主机名的
        //      如果获取不到它的主机名那么是以IP的形式进行返回的
        String name = address1.getHostName();
        System.out.println(name); // 192.168.1.100

        String name1 = address1.getHostName();
        System.out.println(name1); // ROG

        // 3.获取电脑的IP
        String  ip = address1.getHostAddress();
        System.out.println(ip); // 192.168.56.1
    }

}

